Vue.component("select_remebers",{
	props:['account','username'],
	data:function(){
		return {
			contacts:[],
			recents:[],
			contact_opts:[],
			action:null,
			exists_accounts:[],//已存在的群或单聊对象的账号
			exists_role:null,//单用户账号或者群id
			//role:null,//
			exists_type:null,//已存在的聊天类型
		};
	},
	template:loadHTML("components/select_remebers/select_remebers.html"),
	ready:function(){
	//	初始化弹出层组件
		
		
	},
	methods:{
		init:function(data){
			
			
			this.contact_opts=[];
			this.contacts=data.opts.contacts;
			this.recents=data.opts.recents;
			this.action=data.action;
			this.exists_role=data.exists_type;
			this.role=data.role;
			this.exists_accounts=data.exists;
			this.exists_type=data.exists_type;
			if(data.action=="add_remebers"){
				console.log("action:"+data.action);
				if(data.exists_type=="single"){
					console.log("exists_type:"+data.exists_type);
					for(var i=0;i<data.opts.contacts.length;i++){	
						if(data.opts.contacts[i].friaccount==data.exists[0].fri_account){
							continue;
						}
						var item_data={
								username:data.opts.contacts[i].username,
								account:data.opts.contacts[i].friaccount,
								icon_src:data.opts.contacts[i].icon_src,
								selected:false,
						}
						
						
						this.contact_opts.push(item_data);
					}
				}else{
					console.log("exists_type:"+data.exists_type);
					for(var i=0;i<data.opts.contacts.length;i++){
						var flag=true;
						for(var j=0;j<data.exists.length;j++){							
							if(data.exists[j].fri_account==data.opts.contacts[i].friaccount){
								flag=false;
							}				
						}
						if(flag){
							var item_data={
									username:data.opts.contacts[i].username,
									account:data.opts.contacts[i].friaccount,
									icon_src:data.opts.contacts[i].icon_src,
									selected:false,
							}
							
							
							this.contact_opts.push(item_data);
						}
						
					}
				}
			
				
			}else{
				console.log("action:"+data.action);
				for(var i=0;i<data.opts.contacts.length;i++){					
					var item_data={
							username:data.opts.contacts[i].username,
							account:data.opts.contacts[i].friaccount,
							icon_src:data.opts.contacts[i].icon_src,
							selected:false,
					}
					
					
					this.contact_opts.push(item_data);
				}
			}
			
			
			
		this.show();
		},
		show:function(){
			var cansel=this.cansel;
			var flag=false;
			var copmlete_select=this.copmlete_select;
			 $(".select_.ui.modal").modal({
				 onHidden:function(){
					 if(flag){
						 return;
					 }
					 console.log("hidden");
					 cansel();
				 },
				 onApprove :function(){
					 console.log("on");
					 copmlete_select();
					 flag=true;
				 }
			 })
			  .modal('show');
		},
		selected_items:function(a){
			console.log(a.$index);
			
			this.contact_opts[a.$index].selected=this.contact_opts[a.$index].selected==true?false:true;
			
		},
		cansel:function(){
			console.log("取消了");
		},
		copmlete_select:function(){
			console.log("完成选择");
			console.log(this.action);
			var selected_remebers=[
			                       {
			                    	   account:this.account,
			                    	   username:this.username,
			                       }
			                       ];
			
			for(var i=0;i<this.contact_opts.length;i++){
				if(this.contact_opts[i].selected){
					selected_remebers.push({
						account:this.contact_opts[i].account,
						username:this.contact_opts[i].username,
					}
					);
				}
			}
			
			
			
			if(selected_remebers.length==0){
				console.log("未选择成员");
				return;
			}
			if(this.action=="add_remebers"){
				console.log("添加群成员");
				//判断当前群是否存在
				
				if(this.exists_type=="single"){
					console.log("单聊对象转变为群聊，创建群");
					selected_remebers.push({
						account:this.exists_accounts[0].fri_account,
						username:this.exists_accounts[0].username
						
					});
					var group_info={
							name:this.username+","+selected_remebers[1].username+"等人的临时会话",
							admin:this.account,
							remebers:selected_remebers,
					};
					this.create_group(group_info);
				}else{
					console.log("群已存在，添加群成员");
					remebers_json={};
					
					for(var i=0;i<selected_remebers.length;i++){
						remebers_json["account_"+i]=selected_remebers[i].account;
					}
					var str=JSON.stringify(remebers_json);
					
					sendRequest("add_remebers", "post", "json", false, {group_id:this.role,add_remebers:str,senderAccount:this.account}, function(res){
						console.log(res);
						if(res.success){
							//更新群成员,recentlist,chatpanel,
							this.$emit("update_remebers",res.data);
						}
					}.bind(this));
				}
				
				
			}else{
				console.log("创建新聊天");
				//判断当前聊天对象是否存在
				if(selected_remebers.length>2){
					console.log("选择人数大于一人，创建群");
					//构造群基本信息
					var group_info={
							name:this.username+","+selected_remebers[1].username+"等人的临时会话",
							admin:this.account,
							remebers:selected_remebers,
					};
					this.create_group(group_info);
				}else{
					var isExists=false;
					console.log("创建单聊对象");
					for(var i=0;i<this.recents.length;i++){
						if(selected_remebers[1].account==this.recents[i].friaccount){
							isExists=true;
						}
					}
					if(isExists){
						console.log("单聊对象已存在，直接打开聊天窗口");
						
						
						
					}else{
						console.log("单聊对象不存在，创建新联系人");
						var info ={
								friaccount:selected_remebers[1].account
						};
						
						this.create_single_chat(info);
						
					}
				}
				
				
			}
			
			
			
		},
		create_group:function(group_info){
			remebers_json={};
			for(var i=0;i<group_info.remebers.length;i++){
				remebers_json["account_"+i]=group_info.remebers[i].account;
			}
			var str=JSON.stringify(remebers_json);
			sendRequest("create_group", "post", "json", true, {group_name:group_info.name,remebers:str,admin:group_info.admin}, function(res){
				console.log(res);
				if(res.success){
					//群创建成功，添加到联系人列表
					var open_opts={
							show_page:"chat_panel_comp",
							opts:{
								position:res.data.city,
								chat_type:"group",
								username:res.data.name,
								friaccount:res.data.id,
								title:res.data.name,
							},
							
					}
					
					
					this.$emit("curr_right_content",open_opts);
				}
			}.bind(this));
		},
		create_single_chat:function(chat_info){
//			sendRequest("create_chat", "post", "json", true, {account:this.account,friaccount:chat_info.friaccount}, function(res){
//				console.log(res);
//				if(res.success){
//					this.$emit("open_chat_panel");
//				}
//			});
			
			sendRequest("getUserInfo", "post", "json", true, {account:chat_info.friaccount}, function(res){
				if(res.success){
					var open_opts={
							show_page:"chat_panel_comp",
							opts:{
								position:res.data[0].city,
								chat_type:"single",
								username:res.data[0].username,
								friaccount:res.data[0].account,
								title:res.data[0].username,
							},
							
					}
					
					
					this.$emit("curr_right_content",open_opts);
				}
			}.bind(this));
			
		}
		
	},
});